import {request} from '@/utils/request'
import type { HbtPagedResult, HbtSelectOption } from '@/types/common'
import type { 
  HbtPostQuery, 
  HbtPost,
  HbtPostCreate,
  HbtPostUpdate
} from '@/types/identity/post'

/**
 * 获取岗位分页列表
 */
export function getPostList(params: HbtPostQuery) {
  return request<HbtPagedResult<HbtPost>>({
    url: '/api/HbtPost/list',
    method: 'get',
    params
  })
}

/**
 * 获取岗位详情
 */
export function getByIdAsync(postId: string) {
  return request<HbtPost>({
    url: `/api/HbtPost/${postId}`,
    method: 'get'
  })
}

/**
 * 创建岗位
 */
export function createPost(data: HbtPostCreate) {
  return request<number>({
    url: '/api/HbtPost',
    method: 'post',
    data
  })
}

/**
 * 更新岗位
 */
export function updatePost(data: HbtPostUpdate) {
  return request<boolean>({
    url: '/api/HbtPost',
    method: 'put',
    data
  })
}

/**
 * 删除岗位
 */
export function deletePost(postId: string) {
  return request<boolean>({
    url: `/api/HbtPost/${postId}`,
    method: 'delete'
  })
}

/**
 * 批量删除岗位
 */
export function batchDeletePost(postIds: string[]) {
  return request<boolean>({
    url: '/api/HbtPost/batch',
    method: 'delete',
    data: postIds
  })
}

/**
 * 更新岗位状态
 */
export function updatePostStatus(data: { postId: string; status: number }) {
  return request<boolean>({
    url: `/api/HbtPost/${data.postId}/status`,
    method: 'put',
    params: { status: data.status }
  })
}

/**
 * 获取岗位选项列表
 */
export function getPostOptions() {
  return request<HbtSelectOption[]>({
    url: '/api/HbtPost/options',
    method: 'get'
  })
}

/**
 * 获取用户岗位列表
 */
export function getUserPosts(postId: string) {
  return request<any>({
    url: `/api/HbtPost/${postId}/users`,
    method: 'get'
  })
}

/**
 * 分配用户岗位
 */
export function assignUserPosts(postId: string, userIds: string[]) {
  return request<boolean>({
    url: `/api/HbtPost/${postId}/users`,
    method: 'post',
    data: userIds
  })
}

/**
 * 导入岗位数据
 */
export function importPost(file: File) {
  const formData = new FormData()
  formData.append('file', file)
  return request<{ success: number; fail: number }>({
    url: '/api/HbtPost/import',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 导出岗位数据
 */
export function exportPost(params?: HbtPostQuery) {
  return request({
    url: '/api/HbtPost/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
}

/**
 * 获取岗位导入模板
 */
export function getTemplate() {
  return request({
    url: '/api/HbtPost/template',
    method: 'get',
    responseType: 'blob'
  })
}